<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.8.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="单行子查询select * from t_mon_task tmt where tmt.bill_id=(select tpt.bill_id from t_pub_task tpt where tpt.task_sn=&amp;apos;anything&amp;apos;); 多行子查询select * from t_mon_task tmt where tmt.bill_id in (select tpt.">
<meta name="keywords" content="sql,数据库">
<meta property="og:type" content="article">
<meta property="og:title" content="沾ibator的光，sql忘得不能再多了">
<meta property="og:url" content="http://yoursite.com/2017/04/18/沾ibator的光，sql忘得不能再多了/index.html">
<meta property="og:site_name" content="Feng&#39;s Blog">
<meta property="og:description" content="单行子查询select * from t_mon_task tmt where tmt.bill_id=(select tpt.bill_id from t_pub_task tpt where tpt.task_sn=&amp;apos;anything&amp;apos;); 多行子查询select * from t_mon_task tmt where tmt.bill_id in (select tpt.">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-03-29T07:22:29.029Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="沾ibator的光，sql忘得不能再多了">
<meta name="twitter:description" content="单行子查询select * from t_mon_task tmt where tmt.bill_id=(select tpt.bill_id from t_pub_task tpt where tpt.task_sn=&amp;apos;anything&amp;apos;); 多行子查询select * from t_mon_task tmt where tmt.bill_id in (select tpt.">

<link rel="canonical" href="http://yoursite.com/2017/04/18/沾ibator的光，sql忘得不能再多了/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-Hans'
  };
</script>

  <title>沾ibator的光，sql忘得不能再多了 | Feng's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Feng's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/04/18/沾ibator的光，sql忘得不能再多了/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Fengorz">
      <meta itemprop="description" content="A Quiet Departure.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Feng's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          沾ibator的光，sql忘得不能再多了
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2017-04-18 22:56:09" itemprop="dateCreated datePublished" datetime="2017-04-18T22:56:09+08:00">2017-04-18</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-03-29 15:22:29" itemprop="dateModified" datetime="2020-03-29T15:22:29+08:00">2020-03-29</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/数据库/" itemprop="url" rel="index"><span itemprop="name">数据库</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="单行子查询"><a href="#单行子查询" class="headerlink" title="单行子查询"></a>单行子查询</h2><pre><code>select * from t_mon_task tmt where tmt.bill_id=(select tpt.bill_id from t_pub_task tpt where tpt.task_sn=&apos;anything&apos;);
</code></pre><h2 id="多行子查询"><a href="#多行子查询" class="headerlink" title="多行子查询"></a>多行子查询</h2><pre><code>select * from t_mon_task tmt where tmt.bill_id in (select tpt.bill_id from t_pub_task tpt where tpt.task_sn=&apos;anything&apos;);
</code></pre><h2 id="多列子查询"><a href="#多列子查询" class="headerlink" title="多列子查询"></a>多列子查询</h2><pre><code>select tmt.deal_desc from t_mon_task tmt where (tmt.bill_id, tmt.task_id) in (select tpt.bill_id,tpt.task_id from t_pub_task tpt where tpt.task_sn=&apos;anything&apos;);
</code></pre><h2 id="sql函数"><a href="#sql函数" class="headerlink" title="sql函数"></a>sql函数</h2><pre><code>http://blog.csdn.net/jojo52013145/article/details/6998984
</code></pre><h2 id="group-by"><a href="#group-by" class="headerlink" title="group by"></a>group by</h2><blockquote>
<p>   个人最为憎恨的地方就是，网上有很多所谓深入讲解sql语法的各种博文，其引用的example中，表用A表、B表或者table1、table2来表示，没有和实际应用场景配合说明，那这样子你还说个**毛啊。</p>
</blockquote>
<p>group by 将结果集进行分组，一般与多配合聚合函数，sum，count，min，max等一起使用。<br>举个简单的例子，数据表记录学生id，课程名称，课程成绩，一个学生多门课程，多条记录。<br>现在想要计算学生的总成绩，需要通过id进行分组，然后里面sum（成绩）就可以计算出总成绩了</p>
<pre><code>select sum(t.成绩) 成绩 from 表 t group by t.成绩
</code></pre><p>有时候我们再分组的同时需要查询其他字段，这时候可以采用多字段分组。<br>举个简单的例子，基于上面的例子，现在数据表还有一个字段学期（上学期和下学期）。<br>现在要统计分别每个同学上下成绩的总成绩。</p>
<pre><code>select sum(t.成绩) 成绩, t.学期 学期 from 表 t group by t.成绩, t.学期
</code></pre><h2 id="sql-select语句完整的执行顺序"><a href="#sql-select语句完整的执行顺序" class="headerlink" title="sql select语句完整的执行顺序"></a>sql select语句完整的执行顺序</h2><p>1、from子句组装来自不同数据源的数据；<br>2、where子句基于指定的条件对记录行进行筛选；<br>3、group by子句将数据划分为多个分组；<br>4、使用聚集函数进行计算；<br>5、使用having子句筛选分组；<br>6、计算所有的表达式；<br>7、select 的字段；<br>8、使用order by对结果集进行排序。SQL语言不同于其他编程语言的最明显特征是处理代码的顺序。在大多数据库语言中，代码按编码顺序被处理。但在SQL语句中，第一个被处理的子句式FROM，而不是第一出现的SELECT。</p>
<h2 id="内连接-amp-amp-外链接"><a href="#内连接-amp-amp-外链接" class="headerlink" title="内连接 &amp;&amp; 外链接"></a>内连接 &amp;&amp; 外链接</h2><h5 id="内连接"><a href="#内连接" class="headerlink" title="内连接"></a>内连接</h5><p>所谓内连接，可以这么理解，还是引用上面学生的例子，现在两个表tb_student(学生表)，tb_exam(成绩表)，两个表用id主键关联，想要一一列出各个学生的成绩sql如下：</p>
<pre><code>select s.id, s.name, e.course_name, e.score from tb_student s inner join tb_exam e on s.id=e.id;
</code></pre><blockquote>
<p>   未完待续</p>
</blockquote>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/sql/" rel="tag"># sql</a>
              <a href="/tags/数据库/" rel="tag"># 数据库</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2016/11/25/ear-war-jar-区别/" rel="prev" title="ear war jar 区别">
      <i class="fa fa-chevron-left"></i> ear war jar 区别
    </a></div>
      <div class="post-nav-item">
    <a href="/2017/04/22/游侵华日军南京大屠杀遇难同胞纪念馆有感/" rel="next" title="游侵华日军南京大屠杀遇难同胞纪念馆有感">
      游侵华日军南京大屠杀遇难同胞纪念馆有感 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#单行子查询"><span class="nav-number">1.</span> <span class="nav-text">单行子查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#多行子查询"><span class="nav-number">2.</span> <span class="nav-text">多行子查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#多列子查询"><span class="nav-number">3.</span> <span class="nav-text">多列子查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#sql函数"><span class="nav-number">4.</span> <span class="nav-text">sql函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#group-by"><span class="nav-number">5.</span> <span class="nav-text">group by</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#sql-select语句完整的执行顺序"><span class="nav-number">6.</span> <span class="nav-text">sql select语句完整的执行顺序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#内连接-amp-amp-外链接"><span class="nav-number">7.</span> <span class="nav-text">内连接 &amp;&amp; 外链接</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#内连接"><span class="nav-number">7.0.0.1.</span> <span class="nav-text">内连接</span></a></li></ol></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Fengorz</p>
  <div class="site-description" itemprop="description">A Quiet Departure.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">125</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">68</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">87</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fengorz</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>



  















  

  

</body>
</html>
